package panel.home.downBtns
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.display.Stage;
	
	public class DownBtnsGroup extends Sprite
	{
		[Embed(source="../../../assets/home/downBtnChoose.png")]
		private var chooseBackGround:Class;
		
		private var _chooseBitmap:Bitmap;
		
		private var _homeBtn:HomeDownBtn;
		private var _viewSpotBtn:ViewSpotDownBtn;
		private var _eatBtn:EatDownBtn;
		private var _shopBtn:ShopDownBtn;
		private var _roadBtn:RoadDownBtn;
		private var _stage:Stage;
		
		private var _currentChooseBtn:DownBtnBase;
		private var _funcList:Array;
		private var _btnsList:Array;
		public function DownBtnsGroup(stage:Stage)
		{
			_stage = stage;
			super();
		}
		
		public function init(...args):void
		{
			_funcList = [];
			for(var i:int=0;i<args[0].length;i++)
			{
				_funcList.push(args[0][i]);
			}
			_homeBtn = new HomeDownBtn();
			_viewSpotBtn = new ViewSpotDownBtn();
			_eatBtn = new EatDownBtn();
			_shopBtn = new ShopDownBtn();
			_roadBtn = new RoadDownBtn();
			_btnsList = [_homeBtn,_viewSpotBtn,_eatBtn,_shopBtn,_roadBtn];
			
			_homeBtn.clickFunc = onClickBtn;
			this.addChild(_homeBtn);
			
			var distance:Number = _stage.stageWidth / 5 - _homeBtn.width;
			
			_viewSpotBtn.clickFunc = onClickBtn;
			_viewSpotBtn.x = _homeBtn.x + _homeBtn.width + distance;
			this.addChild(_viewSpotBtn);
			
			_eatBtn.clickFunc = onClickBtn;
			_eatBtn.x = _viewSpotBtn.x + _viewSpotBtn.width + distance;
			this.addChild(_eatBtn);
			
			_shopBtn.clickFunc = onClickBtn;
			_shopBtn.x = _eatBtn.x + _eatBtn.width + distance;
			this.addChild(_shopBtn);
			
			_roadBtn.clickFunc = onClickBtn;
			_roadBtn.x = _shopBtn.x + _shopBtn.width + distance;
			this.addChild(_roadBtn);
			resize();
			
			_chooseBitmap = new chooseBackGround();
			_chooseBitmap.scaleX = _chooseBitmap.scaleY = 0.6;
			_homeBtn.changeChooseState(_chooseBitmap,true);
			_currentChooseBtn = _homeBtn;
		}
		
		private function onClickBtn(btn:DownBtnBase):void
		{
//			if(_currentChooseBtn == btn) return;
			_currentChooseBtn.changeChooseState(_chooseBitmap,false); 
			btn.changeChooseState(_chooseBitmap,true);
			_currentChooseBtn = btn;
			var _currentIndex:int = seekIndex(btn);
			if(_funcList[_currentIndex] != null)
			{
				_funcList[_currentIndex]();
			}
		}
		
		private function seekIndex(btn:DownBtnBase):uint
		{
			for(var i:int=0;i<_btnsList.length;i++)
			{
				if(btn == _btnsList[i])
				{
					return i;
				}
			}
			return 0;
		}
		
		private function resize():void
		{
			this.x = (_stage.stageWidth - this.width) / 2;
		}
		
	}
}